package com.kdgz.kdgz.controller;

import com.kdgz.kdgz.beans.Orders;
import com.kdgz.kdgz.sevice.OrdersService;
import com.kdgz.kdgz.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Desc
 * @date 2021/3/19
 */
@RestController
public class OrdersController {
    @Autowired
    OrdersService ordersService;
    /**
     * 添加一个快递
     * @param order 完整的快递信息
     * @return com.kdgz.kdgz.util.Result
     */
    @PostMapping("/order")
    public Result addOrder(@RequestBody Orders order) {
        Integer integer = ordersService.addOrder(order);
        if (integer != 0) {
            return Result.success();
        }else {
            return Result.fail("添加失败！");
        }
    }
    /**
     * 获得所有快递信息
     * @return java.util.List<com.kdgz.kdgz.beans.Orders>
     */
    @GetMapping("/order")
    public List<Orders> getOrders() {
        return ordersService.selectOrders();
    }
    /**
     * 查询某个快递员所属快递信息
     * @param sid 快递员id
     * @return java.util.List<com.kdgz.kdgz.beans.Orders>
     */
    @GetMapping("/orderBySid/{sid}")
    public List<Orders> getOrdersBySid(@PathVariable("sid") Integer sid){
        return ordersService.getOrdersBySid(sid);
    }
    @GetMapping("/orderBySid/{sid}/{status}")
    public List<Orders> getOrdersBySidAndStatus(@PathVariable("sid") Integer sid,@PathVariable("status") Integer status){
        return ordersService.getOrdersBySidAndStatus(sid,status);
    }
    /**
     * 获得某个状态下所有快递信息
     * @param status 状态
     * @return java.util.List<com.kdgz.kdgz.beans.Orders>
     */
    @GetMapping("/order/{status}")
    public List<Orders> getOrdersByStatus(@PathVariable("status") Integer status) {
        return ordersService.getOrdersByStatus(status);
    }
    /**
     * 获得某月的快递数量
     * @param year 年份
     * @param month 月份
     * @return java.util.List<com.kdgz.kdgz.beans.Orders>
     */
    @GetMapping("/order/{year}/{month}")
    public Integer getOrdersByMonth(@PathVariable("year") Integer year, @PathVariable("month") Integer month) {
        return ordersService.totalOrdersByMonth(year,month);
    }
    /**
     * 获得某月的快递运费
     * @param year 年份
     * @param month 月份
     * @return java.util.List<com.kdgz.kdgz.beans.Orders>
     */
    @GetMapping("/orderFee/{year}/{month}")
    public BigDecimal getOrdersFeeByMonth(@PathVariable("year") Integer year, @PathVariable("month") Integer month) {
        return ordersService.getTotalPrice(year,month);
    }
    /**
     * 获得具体某天的快递运费
     * @param year 年份
     * @param month 月份
     * @param day 具体一天
     * @return java.math.BigDecimal
     */
    @GetMapping("/orderFee/{year}/{month}/{day}")
    public BigDecimal getOrdersFeeByMonth(@PathVariable("year") Integer year,
                                          @PathVariable("month") Integer month,
                                          @PathVariable("day") Integer day) {
        return ordersService.totalPriceByDay(year,month,day);
    }
    /**
     * 更新某个快递的信息
     * @param order 快递信息（必须包含快递id,其余传入需要更改的参数即可）
     * @return com.kdgz.kdgz.util.Result
     */
    @PutMapping("/order")
    public Result updateOrder(@RequestBody Orders order) {
        Integer integer = ordersService.updateOrder(order);
        if (integer != 0){
            return Result.success();
        }else {
            return Result.fail("更新失败！");
        }
    }
    /**
     * 根据快递id删除快递
     * @param oid 快递id
     * @return com.kdgz.kdgz.util.Result
     */
    @DeleteMapping("/order/{oid}")
    public Result deleteOrder(@PathVariable("oid") Integer oid) {
        Integer integer = ordersService.deleteOrder(oid);
        if (integer != 0){
            return Result.success();
        }else {
            return Result.fail("更新失败！");
        }
    }

    /**
     * 获得某年某月某个状态下的快递数量
     * @param year 某年
     * @param month 某月
     * @param status 某个状态
     * @return 快递数量
     */
    @GetMapping("/orderStatus/{year}/{month}/{status}")
    public Integer getOrdersByStatusInYearMonth(@PathVariable("year") Integer year,
                                          @PathVariable("month") Integer month,
                                          @PathVariable("status") Integer status){
        return ordersService.totalOrdersByStatus(year,month,status);
    }
}
